class Employee < ActiveRecord::Base
	has_many :tour_groups, :foreign_key => "owner_id"
  
  def Employee.tour_group_count()
    Employee.find_by_sql("SELECT n1.id, n1.name, a.tour_group_count, a.people_count, a.children_count
    FROM employees n1 LEFT OUTER JOIN
    (SELECT n.id, COUNT(*) AS tour_group_count, SUM(people) AS people_count, SUM(children) as children_count
    FROM employees N INNER JOIN tour_groups t ON t.owner_id = n.id
    GROUP BY n.id) AS a ON n1.id = a.id
    ORDER BY a.tour_group_count DESC")
  end
end
